var dms = {};
dms.url = {};
dms.lang = {};
dms.validator = {};
dms.currentPath = '';

/**
* 获得tree node 的 list
*/
dms.url.getFolderTree = 'php/get-folder-tree.php';
/**
* 获得文件列表
*/
dms.url.getList = 'php/get-list.php';
/**
* 获取登录用户信息
*/
dms.url.getInfo = 'php/get-info.php';

dms.url.changePassword = 'php/change-password.php';

dms.url.login = 'php/login.php';
dms.url.logout = 'php/login.php?action=logout'
dms.url.del = 'php/delete.php';

dms.url.addFile = 'php/add-file.php';

dms.url.getFile = 'php/get-file.php';

dms.loaded = {};


/**
* 显示语言。 比如
* lang('find a person') 将显示 dms.lang.findAPerson。如果dms.lang.findAPerson 没有定义。那么显示find a person
*/
function lang(s)
{
	var _s = $.trim(s);
	s = _s.replace(/\b\w+\b/g,function(w)
	{
		return w.substring(0,1).toUpperCase()+w.substring(1);
	}).replace(/\s/g,'').replace(/[^a-z0-9]/ig,'');
	s = s.substring(0,1).toLowerCase()+s.substring(1);
	return (s in dms.lang)?dms.lang[s]:_s;
}


/**
* clear loading mask divs 
* param: int, fade out animation time
* return void
*/
dms.clearLoading = function(t)
{
	if ( t == undefined) t = 400;
	var doms = $('.tab-loading-img').add('.tab-loading-mask');
	if ( t == 0)
	{
		doms.remove();
	}
	else
	{
		doms.stop().fadeOut(t,function()
		{
			doms.remove();
		});
	}
}

dms.load = $.getScript;

dms.initLang = function()
{
	$('[lang]').each(function()
	{
		$(this).html(lang($(this).attr('lang'))).removeAttr('lang');
	});
}

dms.init = function()
{
	Ext.BLANK_IMAGE_URL = 'lib/ext/resources/images/default/s.gif';
	
	$.ajax(
	{
		url: dms.url.getInfo,
		type: 'POST',
		data: { t: Math.random() },
		global: false,
		dataType: 'text',
		error: function()
		{
			$('.tab-loading-img').css('background-image','none').html('Server Error!');
			Ext.Msg.alert(lang('Error'),lang('can not connect to server'));
		},
		success:  function(s)
		{
			try
			{
				eval('var data='+s);
			}catch(e)
			{
				$('.tab-loading-img').css('background-image','none').html(s);
				Ext.Msg.alert(lang('Error'),lang('unknown error'));
				return;
			}
			if (!data.success)
			{
				dms.login.init();
			}
			else
			{
				if (data.username)	$('#dms-user-name').html(data.username);
				if (data.group == 'admin') $('#dms-admin-panel').show();
				//dms.root = data.root;
				dms.load('app/index/index.js',function()
				{
					dms.index.init();
				});
			}
		}
	});
}

dms.login = {};
dms.login.init = function()
{
	function loginSubmit()
	{
		Ext.getCmp('loginForm').form.submit(
		{
			url : dms.url.login,
			success : function(form, action)
			{
				Ext.getCmp('loginWindow').close();
				dms.init();
			},
			failure : function(form, action)
			{
				if (action.failureType == 'client') return;
				Ext.Msg.alert(lang('notice'),lang(action.result.msg),function()
				{
					Ext.getCmp('username').el.focus();
				});
			},
			waitMsg : lang('logining')
		});
	}
	
	var w = new Ext.Window(
	{
		title: lang('login'),
		width: 300,
		height: 140,
		resizable: false,
		padding:10,
		id:'loginWindow',
		renderTo: Ext.getBody(),
		buttonAlign: 'center',
        modal: false,
		items: [
		{
			xtype:'form',
			id:'loginForm',
			baseCls : 'x-plain',
			defaultType:'textfield',
			items:[
			{
				fieldLabel:lang('username'),
				id:'username',
				allowBlank:false
			},
			{
				fieldLabel:lang('password'),
				id:'password',
				inputType: 'password',
				allowBlank:false
			}]
		}],
		buttons: [{
			text: lang('login'),
			handler: loginSubmit
		},
		{
			text:lang('cancel'),
			handler: window.close
		}]
	});
	
	w.el.addKeyListener(13,function()
	{
		loginSubmit();
	});
	
	w.show();
}



/**
* 增加一个Tab页面
* param:  object ，配置参数列表
*  {
* 	 title : string //Tab标题
* 	 js : string //Tab内容页面地址
*  }
*/
dms.addTab = function(attr)
{
	attr = Ext.apply(
	{
		closable:true,
		title:'Loading...',
		id:Ext.id(),
		html:'',
		layout: 'fit'
	},attr);
	var tab;
	if (tab = dms.Tabs.findById(attr.id))
	{
		dms.Tabs.setActiveTab(attr.id);
	}
	else
	{
		var js = attr.js;
		delete(attr.js);
		var params = (attr.params)?attr.params:{};
		delete(attr.params);
		if (typeof js != 'object') js = [js];
		var tab = dms.Tabs.add(attr);
		dms.Tabs.setActiveTab(attr.id);
		dms.load(js,function()
		{
			var name = js[0].replace(/^.*\/([a-z0-9]+)\.js$/i,'$1');
			if (dms[name] && dms[name]['init'])
			{
				dms[name].init.call(tab,params);
			}
			else
			{
				alert('init '+name+' error');
			}
		});
	}
	return tab;
}
